package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.client.ArticleFeign;
import com.heima.user.client.MediaFeign;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-11-12
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService apUserService;

    @Autowired
    private MediaFeign mediaFeign;

    @Autowired
    private ArticleFeign articleFeign;


    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<> ();

        if (dto.getStatus () != null) {
            queryWrapper.eq (ApUserRealname::getStatus , dto.getStatus ());
        }
        //分页
        Page<ApUserRealname> page = new Page<> (dto.getPage () , dto.getSize ());
        Page<ApUserRealname> page1 = this.page (page , queryWrapper);

        return new PageResponseResult (dto.getPage () , dto.getSize () , page1.getTotal () , page1.getRecords ());
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto , int type) { //0 驳回 ，1 通过
        //查询appuser
        ApUser apUser = apUserService.getById (dto.getId ());
        if (apUser == null) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<> ();
        queryWrapper.eq (ApUserRealname::getId , dto.getId ());
        ApUserRealname userRealname = this.getOne (queryWrapper);

        if (type == 0) { //驳回：修改状态 2
            userRealname.setStatus (2);
            userRealname.setReason (dto.getMsg ());
            this.updateById (userRealname);
        } else {//通过：修改状态 9
            //1.成为自媒体人 向media库中的wm_user表中插入数据 远程调用
            WmUser wmUser = saveWmUser (apUser);
            //2.成为文章作者 向artic库中的ap_author表中插入数据 远程调用
            ApAuthor apAuthor = saveApAuthor(apUser,wmUser.getId ());
            //3.给wmUser赋文章作者id
            wmUser.setApAuthorId (apAuthor.getId ());
            mediaFeign.updateWmUser (wmUser);
            //4.修改状态 ApUserRealname的status=9
            userRealname.setStatus (9);
            this.updateById (userRealname);
            apUser.setIsIdentityAuthentication (true);
            apUser.setFlag (1);
            apUserService.updateById (apUser);
        }

        return ResponseResult.okResult ();
    }

    //生成文章作者
    private ApAuthor saveApAuthor(ApUser apUser, Integer wmUser) {
        ApAuthor apAuthor = new ApAuthor ();
        //apAuthor.setId(); //主键自增
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);  //0 爬取数据            1 签约合作商            2 平台自媒体人
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser);
        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        if (responseResult.getCode()!=0) {
            throw new RuntimeException("远程调用失败");
        }else{
            return responseResult.getData();
        }
    }


    private WmUser saveWmUser(ApUser apUser) {
        //1、成为自媒体人  远程调用media微服务 想wm_user表插入数据
        WmUser wmUser = new WmUser ();
        //BeanUtils.copyProperties(apUser,wmUser);
        //wmUser.setId(); //主键自增的 不用处理
        wmUser.setApUserId (apUser.getId ());
        //wmUser.setApAuthorId(); //文章作者id  一会儿再赋值
        wmUser.setName (apUser.getName ());
        wmUser.setPassword (apUser.getPassword ());
        wmUser.setSalt (apUser.getSalt ());
        //wmUser.setNickname(); //无法赋值
        wmUser.setImage (apUser.getImage ());
        //wmUser.setLocation(apUser.get);   //无法赋值
        wmUser.setPhone (apUser.getPhone ());
        wmUser.setStatus (9);  //状态            0 暂时不可用            1 永久不可用            9 正常可用
        //wmUser.setEmail();  //无法赋值
        wmUser.setType (0);  //账号类型            0 个人             1 企业            2 子账号
        wmUser.setScore (0);
        //wmUser.setLoginTime();   //无法赋值

        //远程调用
        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser (wmUser);
        if (responseResult.getCode () != 0) {
            throw new RuntimeException ("远程调用失败");
        } else {
            return responseResult.getData ();
        }

    }
}
